<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ page import="java.util.*"%>
<%@ page import="forum.*"%>
    
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <script type="text/javascript">
	var int = self.setInterval("checkNotifications()", 10000);

	function checkNotifications() {
		check();
	}
	
	function check(){
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", "viewFriends?do=check", true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					var friend = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("friend")[0].firstChild.data;
					if (friend == "y")
						alert("Your friend added post");
					var author = xmlhttp.responseXML.getElementsByTagName("response")[0].getElementsByTagName("author")[0].firstChild.data;
					if (author == "y")
						alert("Post added to your thread");
				} else
					alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
			}
		};
		xmlhttp.send(null);
	}
	
  function showFriend(name){
		var xmlhttp;

		document.getElementById("button"+name).value="Hide Friend";
		document.getElementById("button"+name).onclick=new Function("hideFriend(\""+name+"\")");
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		xmlhttp.open("GET", "viewFriends?do=ajaxShowFriend&name=" + name, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					//process the response
					var mdiv = document.getElementById("friend"+name);
					mdiv.innerHTML = xmlhttp.responseText;
				} else
					alert("Error\n" + xmlhttp.status + ":" + xmlhttp.statusText);
			}
		};
		xmlhttp.send(null);
  }

  function hideFriend(name){
	  document.getElementById("button"+name).value="About Friend";
	  document.getElementById("button"+name).onclick=new Function("showFriend(\""+name+"\")");
	  var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		var mdiv = document.getElementById("friend"+name);
		mdiv.innerHTML = "";
  }
  
  </script>
    <base href="<%=basePath%>">
    
    <title>Friends List</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
<body background="background.jpg" bgcolor="AliceBlue" link="Navy" text="Navy" >
<font face="COMIC SANS MS" >

	<h1 align="center">
		<font size="10"  color="FloralWhite">Friends list</font>
	</h1>
	<br><br><br><br><br>
  <p align="center">
  	<table border="1">
  		<tbody>
			<tr>
				<td colspan="5" align="center">Name</td>
			</tr>
			<%
	  	ArrayList<String> friends = (ArrayList<String>)request.getAttribute("friends");
	  	for (Iterator<String> iter = friends.iterator(); iter.hasNext();) {
	  		String element = (String) iter.next();
			out.println("<tr>");
			out.println("<td align=\"center\"><font size=\"5\" >" + element + "</font></td>");
			out.println("<td><a href=\"editFriend?do=delete&name=" + element + "\">Delete</a></td>");
			out.println("<td>");
	   %>
			<button id="button<%=element%>" type="button" onclick='showFriend("<%=element%>")'>About Friend</button>
			<%out.println("</td>"); out.println("<td>");%>
			<div id="friend<%=element%>"></div>
			<%out.println("</td>"); out.println("</tr>"); } %>
  		</tbody>
  	</table>
  	
  	<br>
	
	<font color="Navy" size ="5">
  	<a href="editFriend?do=add" size=5>Add a new Friend</a>
  	<br>
  	<a href="viewForums" size=5>Back to Forums</a>
	<br>
  	<a href="logout" size=5>logout</a>
	</font>
  	</p>
</font>
  </body>
</html>